package com.abel.bigwater.dataserver.model.h86a

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.dataserver.model.PacketException
import com.abel.bigwater.dataserver.model.PacketNotEnoughException
import io.netty.buffer.Unpooled
import junit.framework.Assert.*
import org.junit.Test
import org.slf4j.LoggerFactory

class H86AHistoryDataTest {

    @Test
    @Throws(PacketException::class)
    fun testGood() {
        val buf = Unpooled.buffer()
        buf.writeBytes(BcdString.toByteArray(
                "AA AA 6A 27 11 16 09 11 17 20 09 44 02 95 54 11 00 00 00 00 00 00 00 37 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 13 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 00 15 00 14 00 11 00 13 00 13 00 16 00 15 00 15 00 15 0D 0A"))
        var d = H86AHistoryData()
        assertTrue(d.parseBuffer(buf, true))
        lgr.info(d.toString())
        lgr.info(d.channelList.joinToString())

        buf.writeBytes(BcdString.toByteArray(
                "AA AA FA 27 11 16 09 11 17 20 1B 44 02 95 60 11 00 00 00 00 00 00 00 37 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 13 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 00 15 00 14 00 11 00 13 00 13 00 16 00 15 00 15 00 15 00 15 00 15 00 15 00 14 00 15 00 15 00 14 00 12 00 16 00 17 00 15 00 16 00 16 00 15 00 15 00 15 00 15 00 16 0D 0A"))
        d = H86AHistoryData()
        assertTrue(d.parseBuffer(buf, true))
        lgr.info(d.toString())
        lgr.info(d.channelList.joinToString())
    }

    @Test
    fun testFalse() {
        val buf = Unpooled.buffer()
        buf.writeBytes(BcdString.toByteArray(
                "AA 6A 27 11 16 "))
        var d = H86AHistoryData()
        assertFalse("should be false caused not this type packet.", d.parseBuffer(buf, true))
    }

    @Test
    fun testNotEnough() {
        val buf = Unpooled.buffer()
        buf.writeBytes(BcdString.toByteArray(
                "AA AA 6A 27 11 16 "))
        var d = H86AHistoryData()
        try {
            d.parseBuffer(buf, true)
            fail("should be not enought")
        } catch (ex: PacketNotEnoughException) {
            // good
        }
    }

    @Test
    fun testAlmostFull() {
        val buf = Unpooled.buffer()
        buf.writeBytes(BcdString.toByteArray(
                "AA AA 6A 27 11 16 09 11 17 20 09 44 02 95 54 11 00 00 00 00 00 00 00 37 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 13 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 00 15 00 14 00 11 00 13 00 13 00 16 00 15 00 15 00 15 "))

        var d = H86AHistoryData()
        try {
            d.parseBuffer(buf, true)
            fail("should be not enought")
        } catch (ex: PacketNotEnoughException) {
            // good
        }
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(H86AHistoryDataTest::class.java)
    }
}